package com.g4.shoppingback.controller;

import com.g4.shoppingback.annotation.WriteLog;
import com.g4.shoppingback.common.PageVo;
import com.g4.shoppingback.common.Result;
import com.g4.shoppingback.pojo.DdSubOrderInfo;
import com.g4.shoppingback.service.DdSubOrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/shoppingback/ddSubOrderInfo")
public class DdSubOrderInfoController {

    @Autowired
    private DdSubOrderInfoService ddSubOrderInfoService;

    /**
     * 根据分页信息或商品名查询订单
     * @param  query,pageNum,orId 关键字,页数,订单号
     * @return 返回分页信息
     */
    @PreAuthorize("hasAnyAuthority('miShop:subOrder:find')")
    @GetMapping
    public Result selectUserByPage(String query,Integer pageNum,String orId){
        PageVo pageResult = ddSubOrderInfoService.selectUserByPaged(query,pageNum,orId);
        return Result.ok(pageResult);
    }

    /**
     * 根据id查询订单明细
     * @param id
     * @return 返回订单明细
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        DdSubOrderInfo ddSubOrderInfo = ddSubOrderInfoService.findById(id);
        return Result.ok(ddSubOrderInfo);
    }

    /**
     * 修改商品详情
     * @param ddSubOrderInfo 传入修改信息
     * @return 返回提示
     */
    @WriteLog("修改订单")
    @PreAuthorize("hasAnyAuthority('miShop:subOrder:update')")
    @PutMapping
    public Result updateSubOrder(@RequestBody DdSubOrderInfo ddSubOrderInfo){
        int row = ddSubOrderInfoService.updateSubOrder(ddSubOrderInfo);
        if(row==1){
            return Result.success("该商品详情修改成功");
        }return Result.error("修改失败");
    }

    /**
     * 根据id删除订单
     * @param id 传入id
     * @return 返回提示
     */
    @WriteLog("删除订单")
    @PreAuthorize("hasAnyAuthority('miShop:subOrder:delete')")
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        int row = ddSubOrderInfoService.deleteById(id);
        if(row==1){
            return Result.success("删除成功");
        }return Result.error("删除失败");
    }
}
